package com.retoc.workflowable.controller;

import com.retoc.workflowable.common.R;
import com.retoc.workflowable.pojo.LeaveReq;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    HistoryService historyService;

    //创建请假申请
    @PostMapping("/leaveRequest")
    public R leaveCreate(@Valid @RequestBody LeaveReq req){
        Map<String,Object> map=new HashMap<>();
        map.put("url", req.getUrl());
        map.put("createrId", req.getCreaterId());
        map.put("managerId", req.getManagerId());
        map.put("gmId", req.getGmId());
        map.put("needGm", req.getNeedGm());
        map.put("date", req.getDate());
        map.put("day", req.getDay());
        map.put("types", req.getTypes());
        map.put("uuid", req.getUuid());
//        2ad4c929-ebec-11ec-808a-48e7da29a915
        ProcessInstance leaveReq = runtimeService.startProcessInstanceByKey("leaveReq", map);
//        leaveReq.getId()
        Task task = taskService.createTaskQuery()
                .processInstanceId(leaveReq.getId())
                .taskAssignee(req.getCreaterId()+"")
                .singleResult();
        taskService.complete(task.getId());
        Map<String,Object> result=new HashMap<>();
        result.put("instanceId",leaveReq.getId());
        result.put("types", req.getTypes());
        result.put("uuid", req.getUuid());
        return R.ok().put("result",result);
    }
//
//    //删除请假申请
//    @DeleteMapping("/DeleteProcessById")
//    public R DeleteProcessById(@Valid @RequestBody DeleteReq req){
//        Task task = taskService.createTaskQuery().processInstanceId(req.getInstanceId()).singleResult();
//        if (task==null){
//            return R.error("实例ID错误！");
//        }
//        runtimeService.deleteProcessInstance(req.getInstanceId(),req.getReason());
////        System.out.println(req.getInstanceId()+"删除成功，原因为："+req.getReason());
//        return R.ok();
//    }
//
//    //审批请假申请
//    @PostMapping("/approvalLeave")
//    public R approvalLeave(@Valid @RequestBody ApproveReq req) {
//        Task task = taskService.createTaskQuery().processInstanceId(req.getInstanceId()).singleResult();
//        //实例ID查不到则返回错误信息
//        if (task==null){
//            return R.error("实例ID错误！");
//        }
//        //如果同意审批，则完成审批任务跳到下一个步骤
//        if ("Approved".equals(req.getApproval())){
//            taskService.complete(task.getId());
//            return R.ok().put("instanceId",task.getProcessInstanceId());
//        }else {
//            //如果不同意审批则删除任务并返回消息
//            runtimeService.deleteProcessInstance(req.getInstanceId(),req.getReason());
//            return R.ok().put("result","reject");
//        }
//    }
//
//    //查询请假申请
//    @PostMapping("/searchLeaveRequest")
//    public R searchLeaveRequest(@Valid @RequestBody CheckInstanceReq req) {
//        List<Task> list = taskService.createTaskQuery().taskAssignee(req.getCheckerId()).list();
//        if (list.isEmpty()){
//            return R.error("用户没有待审批工作");
//        }
//        List<HashMap<String,Object>> result=new ArrayList<>();
//        list.forEach(task -> {
//            HashMap<String,Object> map=new HashMap<>();
//            Object type1 = taskService.getVariable(task.getId(), "type");
////            Object type = runtimeService.getVariable(task.getExecutionId(), "type");
//            map.put("instanceId",task.getProcessInstanceId());
//            map.put("type",type1);
//            result.add(map);
//        });
//        return R.ok().put("result",result);
//    }


//    @PostMapping("/test")
//    public R tst(@Valid @RequestBody TestReq testReq){
//        System.out.println(testReq.getTest());
//        return R.ok().put("result", "success");
//    }

//    26502427-ebef-11ec-808a-48e7da29a915
}
